package org.mq.mymq.nameserver.core;

import io.netty.util.internal.StringUtil;
import org.mq.mymq.common.constants.BrokerConstants;
import org.mq.mymq.nameserver.common.CommonCache;
import org.mq.mymq.nameserver.common.MasterSlaveReplicationProperties;
import org.mq.mymq.nameserver.common.NameServerProperties;
import org.mq.mymq.nameserver.common.TraceReplicationProperties;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * @ClassName PropertiesLoader
 * @Description 属性加载
 * @Author jiarong_ye
 * @Date 2025/6/19 13:56
 * @Version 1.0
 */
public class PropertiesLoader {
    private Properties properties = new Properties();
    public void loadProperties() throws IOException {
        String myMqHome = System.getenv(BrokerConstants.MY_MQ_HOME);
        properties.load(new FileInputStream(new File(myMqHome + "/config/nameserver.properties")));
        NameServerProperties nameServerProperties = CommonCache.getNameserverProperties();
        nameServerProperties.setNameserverPwd(getStr("nameserver.password"));
        nameServerProperties.setNameserverUser(getStr("nameserver.user"));
        nameServerProperties.setNameserverPort(getInt("nameserver.port"));
        nameServerProperties.setReplicationMode(getStrCanBeNull("nameserver.replication.mode"));
        TraceReplicationProperties traceReplicationProperties = new TraceReplicationProperties();
        traceReplicationProperties.setNextNode(getStrCanBeNull("nameserver.replication.trace.next.node"));
        traceReplicationProperties.setPort(getInCanBeNull("nameserver.replication.trace.port"));
        nameServerProperties.setTraceReplicationProperties(traceReplicationProperties);
        MasterSlaveReplicationProperties masterSlaveReplicationProperties = new MasterSlaveReplicationProperties();
        masterSlaveReplicationProperties.setMaster(getStrCanBeNull("nameserver.replication.master"));
        masterSlaveReplicationProperties.setRole(getStrCanBeNull("nameserver.replication.master.slave.role"));
        masterSlaveReplicationProperties.setType(getStrCanBeNull("nameserver.replication.master.slave.type"));
        masterSlaveReplicationProperties.setPort(getInCanBeNull("nameserver.replication.port"));
        nameServerProperties.setMasterSlaveReplicationProperties(masterSlaveReplicationProperties);
        nameServerProperties.print();
        CommonCache.setNameserverProperties(nameServerProperties);
    }

    public String getProperty(String key) {
        return properties.getProperty(key);
    }

    private String getStrCanBeNull(String key) {
        String value = properties.getProperty(key);
        if (value == null) {
            return "";
        }
        return value;
    }

    private String getStr(String key) {
        String value = properties.getProperty(key);
        if (value == null) {
            throw new RuntimeException("配置参数：" + key + "不存在");
        }
        return value;
    }

    private Integer getInCanBeNull(String key) {
        String intValue = getStrCanBeNull(key);
        if (StringUtil.isNullOrEmpty(intValue)) {
            return null;
        }
        return Integer.valueOf(intValue);
    }

    private Integer getInt(String key) {
        return Integer.valueOf(getStr(key));
    }


    public static void main(String[] args) throws IOException {
        PropertiesLoader propertiesLoader = new PropertiesLoader();
        propertiesLoader.loadProperties();
    }
}
